<?php


namespace app\http;
use Swoole\Server;
use Swoole\Websocket\Frame;
use Swoole\WebSocket\Server as WebsocketServer;
use think\Config;
use think\Request;
use think\swoole\contract\websocket\HandlerInterface;

class websocket implements HandlerInterface{
    /** @var WebsocketServer */
    protected $server;

    /** @var Config */
    protected $config;




    /**
     * "onMessage" listener.
     *  only triggered when event handler not found
     *
     * @param Frame $frame
     * @return bool
     */
    public function onMessage(Frame $frame)
    {
        $packet = $frame->data;
        if (Packet::getPayload($packet)) {
            return false;
        }

        $this->checkHeartbeat($frame->fd, $packet);

        return true;
    }



    protected function checkHeartbeat($fd, $packet)
    {
        $packetLength = strlen($packet);
        $payload      = '';

        if ($isPing = Packet::isSocketType($packet, 'ping')) {
            $payload .= Packet::PONG;
        }

        if ($isPing && $packetLength > 1) {
            $payload .= substr($packet, 1, $packetLength - 1);
        }

        if ($isPing) {
            $this->server->push($fd, $payload);
        }
    }

    public function encodeMessage($message){
        // TODO: Implement encodeMessage() method.
    }

    public function onOpen(Request $request)
    {
        echo "队列连接\n";
    }

    public function onClose()
    {
        // TODO: Implement onClose() method.
    }

    protected function push($data) {
        $this->websocket->push($data);
    }

    public function onRequest($req, $respone){
        /*if (!empty($req->get['packet'])) {

        }*/
        $this->
        $respone->end("success");
    }
}